package com.fun.wyunfei.timeline;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.fun.wyunfei.timeline.adapter.TimeLineAdapter;
import com.fun.wyunfei.timeline.bean.Article;
import com.fun.wyunfei.timeline.service.DbService;

import java.util.List;

public class MainActivity extends ListActivity {

    Handler handler = new Handler();
    FloatingActionButton addButton;
    FloatingActionButton settingButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initStateBar();
        FloatingActionButton menuButton = findViewById(R.id.menu);
        addButton = findViewById(R.id.addArticle);
        settingButton = findViewById(R.id.setting);
        menuButton.setOnClickListener(this::showAllButton);
        addButton.setOnClickListener(this::toAddArticleActivity);
        settingButton.setOnClickListener(this::setting);
        handler.post(this::initListAdapter);
    }

    private void setting(View view) {
        startActivity(new Intent(this, SettingActivity.class));
    }

    boolean isShow = true;

    private void showAllButton(View view) {
        if (isShow) {
            isShow = false;
            addButton.show();
            settingButton.show();
            handler.postDelayed(() -> {
                isShow = true;
                addButton.hide();
                settingButton.hide();
            }, 6000);
        } else {
            isShow = true;
            addButton.hide();
            settingButton.hide();
        }
    }

    private void initStateBar() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    @Override
    protected void onResume() {
        super.onResume();
        handler.post(this::initListAdapter);
    }

    public void refreshList() {
        handler.post(this::initListAdapter);
    }

    TimeLineAdapter adapter;
    List<Article> articles;

    private void initListAdapter() {
        DbService dbService = new DbService(getApplicationContext());
        articles = dbService.queryArticle();
        adapter = new TimeLineAdapter(MainActivity.this, articles, this::clickDate);
        setListAdapter(adapter);
    }

    private void clickDate() {

    }

    private void toAddArticleActivity(View view) {
        Log.e("click", "MainActivity: " + view.getId());
        startActivity(new Intent(MainActivity.this, ArticleActivity.class));
    }
}
